<template>
	<div class="allcontent">
		<div class="search">
			<el-form @submit.native.prevent  :model="searchParam" ref="queryFormRef">
				<el-row gutter="10">
					<el-col span="8">
						<el-form-item prop="deptName">
							<el-input  size="small" @keyup.enter.native="search" placeholder="请输入部门名称模糊搜索"  v-model="searchParam.deptName"></el-input>
						</el-form-item>
					</el-col>
					<el-col  span="8">
						<el-form-item prop="status">
							<el-select  size="small" v-model="searchParam.status" style="width:200px" placeholder="请选择状态搜索">
							    <el-option
							      key="1"
							      label="正常"
							      value="1"
							    />
								<el-option
								  key="0"
								  label="禁用"
								  value="0"
								/>
							  </el-select>
						</el-form-item>
					</el-col>
					<el-col  span="8">
						<el-button   size="small" plain icon="Search" type="primary" @click="search">搜索</el-button>
					</el-col>
					<el-col  span="8">
						<el-button  size="small" plain icon="Refresh" type="warning" @click="reset(queryFormRef)">重置</el-button>
					</el-col>
				</el-row>
				<el-row gutter="10" style="margin-top:10px;margin-bottom: 10px;">
					<el-col span="8">
						<el-button  size="small" plain icon="Edit"  v-if="userStore.hasPermission('system:dept:add')" type="success" @click="addDept()">新增</el-button>
						<el-button  size="small" plain type="primary" @click="expenFun()">展开/折叠</el-button>
					</el-col>
				</el-row>
			</el-form>
		</div>
		<div class="el-table">
			<el-table :data="tableData" style="width: 100%;font-size: 13px;"
			row-key="id"
			      border :default-expand-all="expendAll" :key="tableKey" v-loading="loading">
			    <el-table-column prop="deptName" label="部门名称"/>
				<el-table-column prop="sort" label="排序"/>
				<el-table-column prop="status" label="状态">
					<template  #default="scope">
						<el-tag v-if="scope.row.status == 1">正常</el-tag>
						<el-tag type="danger" v-else-if="scope.row.status == 0">禁用</el-tag>
					</template>
				</el-table-column>	
				<el-table-column prop="createTime" label="创建时间">
					<template #default="scope">
						{{formatTimestamp(scope.row.createTime)}}
					</template>
				</el-table-column>
				<el-table-column  label="操作" width="300px">
					<template  #default="scope">
						<el-button  plain icon="Edit" v-if="userStore.hasPermission('system:dept:add')"  size="small" type="success" @click="addDept(scope.row.id)">新增</el-button>
						<el-button  plain icon="Edit" v-if="userStore.hasPermission('system:dept:edit')" size="small" type="primary" @click="editDept(scope.row.id)">编辑</el-button>
						<el-popconfirm  v-if="userStore.hasPermission('system:dept:delete')" 
						    confirm-button-text="是"
						    cancel-button-text="否"
						    :icon="InfoFilled"
						    icon-color="#626AEF"
						    title="是否删除该数据?"
						    @confirm="deleteDept(scope.row)"
						  >
						    <template #reference>
						      <el-button  plain icon="Delete" size="small" type="danger">
								  删除
						      </el-button>
						    </template>
						  </el-popconfirm>
						
					</template>
				</el-table-column>
			</el-table>
		</div>
		<el-drawer size="40%" v-model="saveDeptDialog" :title="deptTitle" width="50%">
			<SaveDept  :t="new Date().getTime()"  :deptId="deptId" :parentId="parentId"  @closeSaveDeptForm="closeSaveDeptForm" @success="success"></SaveDept>
		</el-drawer>
	</div>
</template>

<script setup lang="ts">
	import {ref,reactive, onMounted} from 'vue'
	import SaveDept from './SaveDept.vue'
	import {treelist,remove} from '@/api/dept.ts'
	import type { FormInstance } from 'element-plus'
	import  { ElMessage } from 'element-plus'
	import {useUserStore} from '@/stores/user.ts'
	import {formatTimestamp} from '@/utils/date.ts'
	const userStore = useUserStore()
	
	const searchParam =reactive({
		deptName:null,
		status:null,
	})
	const search=()=>{
		loadData()
	}
	const queryFormRef=ref<FormInstance>();
	const reset = (formEl: FormInstance | undefined) => {
		  if (!formEl) return
		  formEl.resetFields()
		  console.log("fff:"+formEl)
		  loadData();
	}
	interface Dept{
		id:number,
		deptName:string,
		sort:string,
		status:string,
		createTime:Date,
		children?:Dept[],
		hasChildren?:boolean
	}
	let tableData:Dept[] = [];
	const loading = ref(false);
	const loadData=()=>{
		loading.value=true;
		treelist(searchParam).then((res)=>{
			if(res.code === 200){
				tableData = res.data;
			}
			loading.value=false;
		})
	}
	onMounted(()=>{
		loadData();
	})
	
	const saveDeptDialog = ref(false);
	const deptTitle = ref();
	
	// 关闭保存用户弹出框
	const closeSaveDeptForm = ()=> {
	  saveDeptDialog.value = false
	  deptId.value = null;
	  parentId.value = null;
	}
	
	// 提交表单回调函数
	const success = ()=> {
	  saveDeptDialog.value = false;
	  deptId.value = null;
	  parentId.value = null;
	  loadData()
	}
	const deptId = ref()
	const parentId = ref()
	const addDept=(deptId:number)=>{
		deptTitle.value='新增'
		parentId.value=deptId;
		saveDeptDialog.value = true;
	}
	const editDept=(id:number)=>{
		deptTitle.value='编辑'
		deptId.value = id;
		saveDeptDialog.value = true;
	}
	const deleteDept = async (row:Dept) => {
	  if(row.children && row.children.length > 0){
		  ElMessage({
			  type:'warning',
			  message:'当前部门下有子部门，需先删除子部门后，才能删除该部门!'
		  })
		  return;
	  }
	  const ids = [row.id];
	  await remove(ids)
	  loadData();
	}
	const expendAll = ref(false);
	const tableKey = ref();
	const expenFun=()=>{
		tableKey.value = +new Date();
		expendAll.value=!expendAll.value;
	}
</script>

<style scoped>
	.search{
		margin-bottom: 10px;
	}
	.allcontent{
		height: 65vh;
		position: relative;
	}
	.el-table{
		max-height:70%;
		overflow: auto;
	}
	.el-table::-webkit-scrollbar{
		display: none;
	}
</style>